//
//  QSMineViewController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/5/13.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSMineViewController.h"
#import "QSMineHeaderView.h"
#import "QSMyInfoViewController.h"
#import "QSMineCell.h"
#import "QSMineCellModel.h"
#import "QSShareViewController.h"
#import "QSMySetViewController.h"
#import "QSUserInterface.h"
#import "QSEnterpriseArchivesViewController.h"
#import "QSSingInInterface.h"
#import "QSWebViewController.h"
@interface QSMineViewController ()<QSMineHeaderViewDelegate>
@property (nonatomic, strong)QSMineHeaderView *headerView;
@property (nonatomic, strong)NSMutableArray *itemModelArr;
@property (nonatomic, assign)NSInteger bundleCompanyNum;
@end
static NSString *tableCell = @"Cell";
@implementation QSMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"个人中心";
    
    [self configSubView];
    [self getBundleCompanyCount];//请求未读数量
    [self configCellItem];
}

- (void)configSubView{
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.headerView];
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.offset(kScreenWidth * kMineHeaderViewProportion);
    }];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:tableCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"QSMineCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:tableCell];
//    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.headerView.mas_bottom).offset(- kRealValue(20));
//        make.left.right.equalTo(self.view);
//        make.bottom.equalTo(self.view).offset(- kTabBarHeight);
//    }];
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom).offset(- kRealValue(40));
        make.left.equalTo(self.view).offset(kRealValue(10));
        make.right.equalTo(self.view).offset(- kRealValue(10));
        make.bottom.equalTo(self.view).offset(- kTabBarHeight);
    }];
    [self.view bringSubviewToFront:self.tableView];

}

- (void)configCellItem{
    QSMineCellModel *myEnterprise = [[QSMineCellModel alloc]init];
    myEnterprise.leftText = @"我关联的企业";
//    myEnterprise.leftImage = [UIImage imageNamed:@"ic_mine_myEnterprise"];
    
    QSMineCellModel *enterpriseServiceReport = [QSMineCellModel new];
    enterpriseServiceReport.leftText = @"我的企业服务报告";
//    enterpriseServiceReport.leftImage = [UIImage imageNamed:@"ic_mine_servicereport"];

    
    QSMineCellModel *share = [QSMineCellModel new];
    share.leftText = @"分享";
//    share.leftImage = [UIImage imageNamed:@"ic_mine_share"];

    
    QSMineCellModel *set = [QSMineCellModel new];
    set.leftText = @"系统设置";
//    set.leftImage = [UIImage imageNamed:@"ic_mine_set"];

    
    [self.itemModelArr addObjectsFromArray:@[myEnterprise, enterpriseServiceReport, share, set]];
    NSLog(@"%@", self.itemModelArr);
    [self.tableView reloadData];
}

#pragma mark - Lazy Load
- (QSMineHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [QSMineHeaderView new];
        _headerView.delegate = self;
    }
    return _headerView;
}

- (NSMutableArray *)itemModelArr{
    if (!_itemModelArr) {
        _itemModelArr = [NSMutableArray array];
    }
    return _itemModelArr;
}

#pragma mark - QSMineHeaderViewDelegate
//点击背景图
- (void)mineHeaderView:(QSMineHeaderView *)headerView didSelectBackgroundPicture:(NSInteger)subIndex{
    QSMyInfoViewController *vc = [QSMyInfoViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableCell];
    QSMineCell *cell = [tableView dequeueReusableCellWithIdentifier:tableCell];
    cell.model = self.itemModelArr[indexPath.row];
    cell.companyNum = self.bundleCompanyNum;
    cell.backgroundColor = [UIColor QSColorWhiteFFFFFF];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemModelArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kRealValue(50);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    QSMineCellModel *model = self.itemModelArr[indexPath.row];
    if ([model.leftText isEqualToString:@"我关联的企业"]) {
//        QSWebViewController *webVc = [QSWebViewController new];
//        webVc.URLString = [NSString stringWithFormat:@"%@#/home?allToken=%@&platform=iOS&isContactEnterprise=1", KH5Prefix, kCurrentUser.allToken];
//        webVc.hideNavigationBar = YES;
        QSEnterpriseArchivesViewController *vc = [QSEnterpriseArchivesViewController new];
        vc.isContactEnterprise = @"&isContactEnterprise=1";
        vc.strUrl = [NSString stringWithFormat:@"%@#/home?allToken=%@&platform=iOS&isContactEnterprise=1", KH5Prefix, kCurrentUser.allToken];
        [self.navigationController pushViewController:vc animated:YES];
//        [QSAppKeyWindow showAutoHideHudWithText:@"我关联的企业"];
    }else if ([model.leftText isEqualToString:@"我的企业服务报告"]){
        [QSAppKeyWindow showAutoHideHudWithText:@"数据正在累积中，敬请期待"];
    }else if ([model.leftText isEqualToString:@"分享"]){
        QSShareViewController *vc = [QSShareViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([model.leftText isEqualToString:@"系统设置"]){
        QSMySetViewController *vc = [QSMySetViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self getBundleCompanyCount];//请求绑定企业数量
}

- (void)getBundleCompanyCount{
    
    [QSSingInInterface getShowAllCompanyAndBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, NSString * _Nonnull result) {
        [QSAppKeyWindow hideHud];
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            [QSUserInterface getBundleCompanyNumWithEpAllCompany:result AndBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, NSDictionary * _Nonnull result) {
                if (rspStatusAndMessage.code == kResponseSuccessCode) {
                    self.bundleCompanyNum = [(NSString *)result integerValue];
                    NSLog(@"%@", result);
                    [self.tableView reloadData];
                }
            }];
        }
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
